#!/bin/bash

old_modules=("from silo_aot" "from silo_seeds" "from silo_aot" "from silo_aop" "from silo_ext" "from silo_tx" "from silo_core" "from silo_cdbc")
new_module="from silo_framework"

# old context
old_context="from silo_framework import silo.seeds.context"
new_context="from silo_framework import silo.context"

# 递归遍历当前目录下所有文件并进行替换
function search_and_replace {
    for file in "$1"/*; do
        if [ -d "$file" ]; then
            search_and_replace "$file"
        elif [ -f "$file" ]; then
            for old_str in "${old_modules[@]}"; do
                # 使用 sed 命令替换字符串，并覆盖原文件
                sed -i "s/$old_str/$new_module/g" "$file"
            done
            echo "Replaced strings in $file"
        fi
    done
}

# 递归遍历当前目录下所有文件并进行替换
function search_and_replace_context {
    for file in "$1"/*; do
        if [ -d "$file" ]; then
            search_and_replace_context "$file"
        elif [ -f "$file" ]; then
                # 使用 sed 命令替换字符串，并覆盖原文件
                sed -i "s/$old_context/$new_context/g" "$file"
            echo "Replaced strings in $file"
        fi
    done
}

# 调用递归函数，从当前目录开始遍历
search_and_replace "./src"
search_and_replace_context "./src"